<template>
	<!-- 搜索页面头部 -->
	<SkeletonScreen v-if="loading"></SkeletonScreen>
	<view class="search">
		<view class="search-ai" @click="AiClick">
			<!-- <image class="ai-img" src="/static/icon/ai.png"></image> -->
			<uni-icons type="back" color="#ff0000" size="35"></uni-icons>
		</view>
		<!-- cancelText="返回" -->
		<uni-search-bar style="width: 60%;" bgColor="#969696" placeholder="直接告诉我哦！" textColor="#000" :radius="20"
			:cancelButton="always" @confirm="search" :focus="true" v-model="searchValue" @blur="blur" @focus="focus"
			@input="input" @cancel="cancel" @clear="clear">
		</uni-search-bar>
	</view>
	<view style="width: 100%; height: 180rpx;"></view>

	<!-- 历史 热门  -->
	<view v-if="inputBollean == 1">
		<!-- 搜索历史标题 -->
		<view @click="clearSearchHistory">
			<max-title-min icon="trash" title="搜索历史"></max-title-min>
		</view>

		<!-- 搜索历史内容 -->
		<view class="textList">
			<view class="list-text" v-for="item in listTitle" @click="listTitleClick(item)">{{item}}</view>
		</view>
		<!-- 热门搜索 -->
		<max-title-min title=" 热门搜索" :isIcon="false"></max-title-min>
		<!-- 下部的热门搜索内容 -->
		<scroll-view class="scroll-nav" scroll-x>
			<scroll-search :list="swiperListA" nav="电影"></scroll-search>
			<scroll-search :list="swiperListB" nav="剧集"></scroll-search>
			<scroll-search :list="swiperListC" nav="综艺"></scroll-search>
			<scroll-search :list="swiperListD" nav="动画"></scroll-search>
		</scroll-view>

	</view>

	<!-- input 一字一字的搜索 -->
	<view v-if="inputBollean == 2 ">
		<scroll-search :list="inputValue" :isSearch="false" iconleft="search" iconright="forward"></scroll-search>
	</view>

	<!-- 有搜索结果 -->
	<view v-if="inputBollean == 3 ">
		<view class="image-eight-Three">
			<my-img-y-Three class="item" v-for="item in searchTrueValue" :type="item.type" :images="item.imageUrl"
				:tags="item.tags" :name="item.name" :bifen="item.bifen" :biao="item.biao" :episode="item.episode"
				:episodes="item.episodes" :urlId="item.id"></my-img-y-Three>
		</view>
		<view style="display: flex; justify-content: center; align-items: center; padding: 30rpx 0; color: #464646;">
			<view>到此为止...</view>
		</view>
	</view>

	<!-- 无搜索结果 -->
	<view v-if="inputBollean == 4 ">
		<view class="none-search">
			<image src="/static/none.png"></image>
		</view>
		<!-- 热门搜索 -->
		<max-title-min title=" 热门搜索" :isIcon="false"></max-title-min>
		<!-- 下部的热门搜索内容 -->
		<scroll-view class="scroll-nav" scroll-x>
			<scroll-search :list="swiperListA" nav="电影"></scroll-search>
			<scroll-search :list="swiperListB" nav="剧集"></scroll-search>
			<scroll-search :list="swiperListC" nav="综艺"></scroll-search>
			<scroll-search :list="swiperListD" nav="动画"></scroll-search>
		</scroll-view>
	</view>


</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		searchByTitle,
		swiperimgsBanner
	} from '@/API/searchApis.js';

	const nextPage = ref(1);
	const searchValue = ref(''); // 使用 ref 创建响应式数据
	const inputBollean = ref(1); //一字一句查询显示开关
	const inputValue = ref([]); //input 获取的结果
	const swiperListA = ref([]); // 热门电影
	const swiperListB = ref([]); // 热门剧集
	const swiperListC = ref([]); // 热门综艺
	const swiperListD = ref([]); // 热门动画

	const listTitle = ref([]); //搜索历史
	const loading = ref(true)

	const searchTrueValue = ref([]); //确定搜索

	// 模糊查询
	const searchByTitleFunction = async (name, pageNum, pageSize) => {
		let res = await searchByTitle(name, pageNum, pageSize);
		nextPage.value = res.nextPage;
		console.log("搜索结果：", res);
		return res;
	}

	// 电影热门数据
	const SwiperlistBImg = async () => {
		let resA = await swiperimgsBanner(1, '');
		let resB = await swiperimgsBanner(2, '');
		let resC = await swiperimgsBanner(3, '');
		let resD = await swiperimgsBanner(4, '');

		swiperListA.value = resA.data.list;
		swiperListB.value = resB.data.list;
		swiperListC.value = resC.data.list;
		swiperListD.value = resD.data.list;

		console.log("电影热门数据:", resA);
		getSearchHistory();
		loading.value = false
	}
	// 电影热门数据
	SwiperlistBImg();

	// 搜索键进行搜索
	async function search() {
		loading.value = true
		console.log("searchValue:", searchValue.value);
		saveSearchHistory(searchValue.value); //缓存搜索历史
		if (!searchValue.value) {
			uni.showToast({
				title: '输入关键词搜索！',
				icon: 'none'
			})
			inputBollean.value = 1;
			return;
		}

		const row = await searchByTitleFunction(searchValue.value, 1, 100);
		console.log("list:", row.total);
		if (row.total == 0) {
			inputBollean.value = 4;
			loading.value = false;
			console.log("确实0")
			return;
		}
		searchTrueValue.value = row.list;
		inputBollean.value = 3;
		for (let i = 0; i < searchTrueValue.value.length; i++) {
			if (searchTrueValue.value[i].type == 1) {
				searchTrueValue.value[i].type = '电影'
			} else if (searchTrueValue.value[i].type == 2) {
				searchTrueValue.value[i].type = '剧集'
			} else if (searchTrueValue.value[i].type == 3) {
				searchTrueValue.value[i].type = '综艺'
			} else if (searchTrueValue.value[i].type == 4) {
				searchTrueValue.value[i].type = '动漫'
			}

		}
		loading.value = false
	};

	// input查询
	async function input(res) {
		console.log('----input:', res);
		inputBollean.value = 1;

		if (res) {
			const row = await searchByTitleFunction(res, 1, 10);
			console.log("inputRow", row);
			if (!row) {
				inputBollean.value = 1;
				uni.showToast({
					title: '不支持英文搜索！',
					icon: 'none'
				})
			}

			inputValue.value = row.list;
			inputBollean.value = 2;
			for (let i = 0; i < inputValue.value.length; i++) {
				if (inputValue.value[i].type == 1) {
					inputValue.value[i].type = '电影'
				} else if (inputValue.value[i].type == 2) {
					inputValue.value[i].type = '剧集'
				} else if (inputValue.value[i].type == 3) {
					inputValue.value[i].type = '综艺'
				} else if (inputValue.value[i].type == 4) {
					inputValue.value[i].type = '动漫'
				}

			}
		}

	};
	// 获取搜索历史
	function getSearchHistory() {
		listTitle.value = uni.getStorageSync('searchHistoryList');
		console.log("listTitle.value：", listTitle.value[0]);
	}
	// 缓存搜索历史
	function saveSearchHistory(query) {
		const key = 'searchHistoryList';

		// 获取已有的缓存数据
		let historyList = uni.getStorageSync(key) || [];

		// 去重：如果已有该关键词，先删除旧的
		historyList = historyList.filter(item => item !== query);

		// 添加到最前面
		historyList.unshift(query);

		// 限制最多保存 10 条
		if (historyList.length > 20) {
			historyList.pop();
		}

		// 再次保存
		uni.setStorageSync(key, historyList);
	}
	// 清理搜索历史
	function clearSearchHistory() {
		uni.setStorageSync('searchHistoryList', []);
		listTitle.value = [];
	}

	function listTitleClick(e) {
		console.log("点击历史：", e);
		searchValue.value = e;
		search();
	}


	function clear(res) {
		getSearchHistory();
	};

	function blur(res) {

	};

	function focus(e) {

	};
	// 返回键返回
	function cancel(res) {
		console.log("点击返回按钮")
		getSearchHistory();
		uni.navigateBack({
			delta: 1 // 返回上一个页面
		});
	}

	// ai图标
	function AiClick() {
		getSearchHistory();
		uni.navigateBack({
			delta: 1 // 返回上一个页面
		});
	}
</script>

<style scoped lang="scss">
	.search {
		display: flex;
		position: fixed;
		align-items: end;
		justify-content: start;
		padding: 0 30rpx;
		box-sizing: border-box;
		width: 100%;
		height: 180rpx;
		background: #000;
		z-index: 10;
		/* #ifdef H5 */
		top: 84rpx;
		height: 100rpx;

		/* #endif */
		.search-ai {
			width: 90rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 100rpx;

			.ai-img {
				width: 65rpx;
				height: 65rpx;
			}
		}
	}

	.searchList {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		padding-bottom: 10rpx;
		box-sizing: border-box;
	}

	.textList {
		width: 100%;
		box-sizing: border-box;
		padding: 0 30rpx;
		display: flex;
		align-items: center;
		flex-wrap: wrap;

		.list-text {
			margin-right: 8rpx;
			margin-top: 8rpx;
			font-size: 20rpx;
			background-color: #1d1d1d;
			border-radius: 20rpx;
			padding: 8rpx 15rpx;
		}
	}

	.scroll-nav {
		white-space: nowrap;
		white-space: nowrap;
		overflow-x: auto;
		width: 100%;
	}

	.scroll-nav ::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
		display: none;
	}

	.image-eight-Three {
		padding: 30rpx;
		display: grid;
		gap: 15rpx;
		grid-template-columns: repeat(3, 1fr);

		.item {
			overflow: hidden;
			/* 防止内容溢出 */
			position: relative;
			width: 100%;
			/* 图片宽度填充整个容器 */
			height: auto;
			/* 根据宽度自动调整高度 */
			display: block;
			/* 移除图片下方默认的空白间隙 */
			object-fit: cover;
			/* 保证图片完全覆盖容器，可能会裁剪图片 */
		}
	}

	.none-search {
		display: flex;
		align-items: center;
		justify-content: center;

	}
</style>